package akash.java.code;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class LargeSum
{
    public static void main(String[] args) throws IOException
    {   
        LargeSum largeSum = new LargeSum();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter first number :");
        String number1 = reader.readLine();
        System.out.println("Enter Second number :");
        String number2 = reader.readLine();
        StringBuffer num1 = new StringBuffer(number1);
        StringBuffer num2 = new StringBuffer(number2);
        System.out.println("the sum of these two numbers is :" + largeSum.addNumbers(num1, num2).toString());

    }

    public  StringBuffer addNumbers(StringBuffer num1, StringBuffer num2)
    {
        num1.reverse();
        num2.reverse();

        int length = num1.length() < num2.length() ? num1.length() : num2.length();
        int digit1 = 0;
        int digit2 = 0;
        int carry = 0;
        int sum = 0;
        StringBuffer result = new StringBuffer();
        NumCarry numCarry = new NumCarry();
        for(int i = 0; i < length; i++)
        {
            digit1 = Integer.parseInt(num1.substring(i, i + 1));
            digit2 = Integer.parseInt(num2.substring(i, i + 1));
            sum = digit1 + digit2 + carry;
            numCarry.reSetNumCarry(sum);
            carry = numCarry.getCarry();
            result.append(numCarry.getNum());
        }
        StringBuffer biggerNumber = num1.length()>num2.length()?num1:num2;
        for(int i = length; i < biggerNumber.length(); i++)
        {
            digit1 = Integer.parseInt(biggerNumber.substring(i, i + 1));
            sum = digit1  + carry;
            numCarry.reSetNumCarry(sum);
            carry = numCarry.getCarry();
            result.append(numCarry.getNum());
        }
        if(carry>0){
            result.append(carry);
        }
        return result.reverse();
    }
}
